package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加菜品和套餐
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //设置用户id，指定当前是哪个用户的购物车数据
        Long userId = BaseContext.getCurrentId();
        //添加修改时间
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCart.setUserId(userId);
        //查询当前菜品和套餐是否在数据库中
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(shoppingCart.getDishId()!=null){
            //传过来的是菜品
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());

        }else{
            //传过来的是套餐
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart shoppingCartServiceOne = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
        //如果在数量就加一
        if(shoppingCartServiceOne!=null){
            shoppingCartServiceOne.setNumber(shoppingCartServiceOne.getNumber()+1);
            shoppingCartService.updateById(shoppingCartServiceOne);
        }else{
            //不在默认就是一
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            shoppingCartServiceOne=shoppingCart;
        }



        return R.success(shoppingCartServiceOne);
    }


    /**
     * 查询该用户购物车所有商品
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,userId);

        //查询
        List<ShoppingCart> UserShoppingCartList = shoppingCartService.list(shoppingCartLambdaQueryWrapper);
        return R.success(UserShoppingCartList);
    }

    //http://localhost:8080/shoppingCart/sub

    /**
     * 减少用户菜品和套餐的数量
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    //public R<List<ShoppingCart>> sub(@RequestBody ShoppingCart shoppingCart){
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        //获取用户的id根据id和要删除的菜品或者套餐id删除
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //添加修改时间
        shoppingCart.setCreateTime(LocalDateTime.now());

        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //用户id查找条件
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
        //套餐id存在条件
        shoppingCartLambdaQueryWrapper.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        //菜品id存在条件
        shoppingCartLambdaQueryWrapper.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());

        ShoppingCart shoppingCartOne = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);

        Integer number = shoppingCartOne.getNumber();
        number--;
        if(number<=0){
            //数量小于0时就从购物车中移除
            shoppingCartService.removeById(shoppingCartOne);
            shoppingCartOne.setNumber(0);
        }else{
            shoppingCartOne.setNumber(number);
            shoppingCartService.updateById(shoppingCartOne);
        }



        return R.success(shoppingCartOne);

    }

    //http://localhost:8080/shoppingCart/clean
    @DeleteMapping("/clean")
    public R<String> clean(){
        Long userId = BaseContext.getCurrentId();
        //设置条件userId
        LambdaUpdateWrapper<ShoppingCart> shoppingCartLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        shoppingCartLambdaUpdateWrapper.eq(ShoppingCart::getUserId,userId);
        shoppingCartService.remove(shoppingCartLambdaUpdateWrapper);
        return R.success("删除成功");
    }
}
